<?php

if (!function_exists('custom_path_file')) {
    /**
     * 获取指定到目录下的php文件
     * @param $path
     * @param string $suffix
     * @return array
     */
    function custom_path_file($path, $suffix = '.php'):array{
        $files = [];
        if (is_dir(base_path().'/'.$path)) {
            foreach (scandir(base_path().'/'.$path) as $dir) {
                if ('.' !== $dir[0]) {
                    if (is_dir(base_path().'/'.$path.'/'.$dir)) {
                        $files = array_merge($files, custom_path_file($path.'/'.$dir));
                    } else {
                        if (substr($dir, -4) == $suffix) {
                            $files[] = $path.'/'.$dir;
                        }
                    }
                }
            }
        }

        return $files;
    }
}

if(!function_exists('custom_second_trans')){

    function custom_second_trans(int $seconds) : string {
        $d = floor($seconds / (3600*24));
        $h = floor(($seconds % (3600*24)) / 3600);
        $m = floor((($seconds % (3600*24)) % 3600) / 60);
        if($d>'0'){
            $times  = $d.'天'.$h.'小时'.$m.'分钟';
        }else{
            if($h!='0'){
                $times = $h.'小时'.$m.'分钟';
            }else{
                if($m!=0){
                    $times = $m.'分钟';
                }else{
                    $times = $seconds.'秒';
                }
            }
        }

        return $times;
    }
}

if(!function_exists('custom_account_type')){
    /**
     * 为之后的登陆账号做准备
     * 获取到账号类型
     * @param $data
     * @return string
     */
    function custom_account_type($data):string{
        if(!empty($data) && (is_string($data) || is_integer($data))){
            if(preg_match("/^(\w)+(\.\w+)*@(\w)+((\.\w+)+)$/",$data)){
                return 'email';
            }else if(preg_match("/^[1]([3-9])[0-9]{9}$/",$data)){
                return 'phone';
            }else{
                return 'username';
            }
        }

        return 'username';
    }
}

if(!function_exists('customer_return_success')){
    /**
     * @Notes:返回正常数据
     *
     * @param string $message
     * @param array $data
     * @return array
     * @Author: smile
     * @Date: 2021/7/18
     * @Time: 17:49
     */
    function customer_return_success(string $message, array $data = []): array{
        return [
            'status'  => 'success',
            'code'    => 200,
            'message' => $message,
            'data'    => $data
        ];
    }
}

if (!function_exists('customer_return_error')){
    /**
     * @Notes:返回错误数据
     *
     * @param string $message
     * @param array $data
     * @return array
     * @Author: smile
     * @Date: 2021/7/18
     * @Time: 17:49
     */
    function customer_return_error(string $message, array $data = []) : array{
        return [
            'status'  => 'error',
            'code'    => 500,
            'message' => $message,
            'data'    => $data
        ];
    }
}


if(!function_exists('custom_array_tree')){
    /**
     * @describe 数组无极限分类
     * @param array $array
     * @param string $pk
     * @param string $pid
     * @param string $child
     * @param int $root
     * @return array
     * @throws Exception
     */
    function custom_array_tree(array $array,string $pk='id',string $pid='p_id',string $child='child',int $root=0):array{
        if(!empty($array) && !empty($pk) && !empty($pid) && !empty($child)){
            $tree = array();
            if(is_array($array)) {
                $refer = array();
                foreach ($array as $key => $data) {
                    $refer[$data[$pk]] =& $array[$key];
                }
                foreach ($array as $key => $data) {
                    $parentId = $data[$pid];
                    if ($root == $parentId) {
                        $tree[$data[$pk]] =& $array[$key];
                    }else{
                        if (isset($refer[$parentId])) {
                            $parent =& $refer[$parentId];
                            $parent[$child][] =& $array[$key];
                        }
                    }
                }
            }
            return $tree;
        }
        return $array;
    }
}

if (!function_exists('customer_analysis_string')) {
    /**
     * @Notes:解析指定的字符串
     *
     * @param string $startString
     * @param string $endString
     * @param string $string
     * @param array $parameters
     * @return string
     * @Author: smile
     * @Date: 2021/9/13
     * @Time: 16:49
     */
    function customer_analysis_string(string $startString,string $endString,string $string,array $parameters) : string{
        if (empty($startString) || empty($endString) || empty($parameters)) {
            return $string;
        }

        $pattern = '/';

        for ($i = 0; $i < strlen($startString) ; $i++) {
            $pattern .= '\\'.$startString[$i];
        }

        $pattern = $pattern.'[^}]+';

        for ($i = 0; $i < strlen($endString) ; $i++) {
            $pattern .= '\\'.$endString[$i];
        }

        $pattern = $pattern.'/';

        return preg_replace_callback($pattern,function ($item) use ($parameters,$startString,$endString){
            $item = current($item);

            $name = ltrim($item,$startString);
            $name = rtrim($name,$endString);

            return $parameters[$name] ?? '';
        },$string);
    }
}
